package cn.itcast.controller.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

/**
 * 发送消息的工具类
 */
@Component
public class EmailMessageProducer {
    // 注入JmsTemplate
    @Autowired
    private JmsTemplate jmsTemplate;

    // 发送消息
    public void sendEmailMessage(String to,String subject,String content){
        jmsTemplate.send("email", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                MapMessage mapMessage = session.createMapMessage();
                mapMessage.setString("to",to);
                mapMessage.setString("subject",subject);
                mapMessage.setString("content",content);
                return mapMessage;
            }
        });
    }

}
